﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace RM2012Mobile
{
    public partial class frmInvoiceDetail : Form
    {
        #region Fields
        RM2012Service.RM2012Service objService = new RM2012Service.RM2012Service();
        RM2012Service.InvoiceInfo infoUpdate;
        RM2012Service.InvoiceDetailInfo detailDelete;
        DataSet ds;

        public static string tablesIdPublic = "NOTHING";
        public static string tablesNamePublic = "NOTHING";//--
        public static string invoiceIdPublic = "NOTHING";//--
        public static string menuIdPublic = "NOTHING";//--
        public static string menuNamePublic = "NOTHING";//--
        public static string objectNamePublic = "NOTHING";//
        public static string actionPublic = "NOTHING";//
        public static string numOldPublic = "NOTHING";
        public static string numNewPublic = "NOTHING";
        public static string usernamePublic = Utilities.UsernamePublic;
        public static string fullnamePublic = Utilities.FullNamePublic;
        public static string groupnamePublic = Utilities.GroupNamePublic;
        #endregion

        public frmInvoiceDetail()
        {
            InitializeComponent();
        }

        private void frmInvoiceDetail_Load(object sender, EventArgs e)
        {
            try
            {
                string invoiceId = Utilities.InvoiceIdPublic;
                string tablesName = Utilities.TablesNamePublic;
                txtTablesName.Text = tablesName;
                txtInvoiceId.Text = invoiceId;
                tablesNamePublic = Utilities.TablesNamePublic;//--
                invoiceIdPublic = Utilities.InvoiceIdPublic;//--
                UpdateControl(invoiceId);
                RefreshGrid(invoiceId);
            }
            catch
            {
            	
            }
        }

        public void RefreshGrid(string invoiceId)
        {
            ds = new DataSet();
            ds = objService.GetInvoiceDetailByInvoiceId(invoiceId);
            gridInvoiceDetail.DataSource = ds.Tables[0];
            if (ds.Tables[0].Rows.Count > 0)
            {
                menuIdPublic = gridInvoiceDetail[0, 0].ToString();
                menuNamePublic = gridInvoiceDetail[0, 1].ToString();
            }
        }

        public void UpdateControl(string invoiceId)
        {
            DataSet dsInvoice = objService.GetAllInvoiceById(invoiceId);
            DataTable dtInvoice = dsInvoice.Tables[0];
            string total = dtInvoice.Rows[0]["Total"].ToString();
            total = total.Substring(0, total.LastIndexOf("."));
            txtPaid.Text = total;
        }

        public double GetTotal(string invoiceId)
        {
            double total = 0.0;
            DataSet dsDetail = objService.GetInvoiceDetailByInvoiceId(invoiceId);
            DataTable dtDetail = dsDetail.Tables[0];
            if (dtDetail.Rows.Count > 0)
            {
                for (int i = 0; i < dtDetail.Rows.Count; i++)
                {
                    total = total + Convert.ToDouble(dtDetail.Rows[i]["Price"].ToString()) * Convert.ToDouble(dtDetail.Rows[i]["Quantity"].ToString());
                }
            }
            return total;
        }

        private void menuItemUpdate_Click(object sender, EventArgs e)
        {
            btnUpdate_Click(sender, e);
        }

        private void menuItemAddMenu_Click(object sender, EventArgs e)
        {
            btnAdd_Click(sender, e);
        }

        private void menuItemDelete_Click(object sender, EventArgs e)
        {
            btnDelete_Click(sender, e);
        }

        private void menuItemSendRequest_Click(object sender, EventArgs e)
        {
            btnPay_Click(sender, e);
        }

        private void menuItemClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            RefreshGrid(txtInvoiceId.Text);
            UpdateControl(txtInvoiceId.Text);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Utilities.InvoiceIdPublic = txtInvoiceId.Text;
            Utilities.TablesNamePublic = txtTablesName.Text;

            frmUpdateInvoice frmUpdate = new frmUpdateInvoice();
            frmUpdate.Show();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                //Prepare
                infoUpdate = new RM2012Service.InvoiceInfo();
                detailDelete = new RM2012Service.InvoiceDetailInfo();
                string invoiceId = txtInvoiceId.Text;
                //Delete menu
                detailDelete.InvoiceId = invoiceId;
                detailDelete.MenuId = menuIdPublic;
                objService.DeleteInvoiceDetail(detailDelete);
                //Send info to Server for Logging
                objectNamePublic = "MENU";
                actionPublic = "DELETE";
                ServerLoggingDelete();
                //Update Invoice Total
                double total = GetTotal(invoiceId);
                infoUpdate.InvoiceId = invoiceId;
                infoUpdate.Total = total;
                objService.UpdateInvoiceTotal(infoUpdate);
                //Refresh Control
                RefreshGrid(invoiceId);
                UpdateControl(invoiceId);
            }
            catch
            {

            }
        }

        public void ServerLoggingDelete()
        {
            Thread tClient = new Thread(new ThreadStart(DeleteMenuLog));
            tClient.Start();
        }

        public void DeleteMenuLog()
        {
            try
            {
                string time = Utilities.GetNow();
                string message = Utilities.CreateMessage(tablesIdPublic, tablesNamePublic, invoiceIdPublic, menuIdPublic, menuNamePublic, objectNamePublic, actionPublic, numOldPublic, numNewPublic, usernamePublic, fullnamePublic, groupnamePublic, time);

                MyUdpClient client = new MyUdpClient();
                client.OnConnect(message);
            }
            catch(Exception ex)
            {
                MessageBox.Show("Chưa gửi được !" + ex.Message);
            }
        }

        private void btnPay_Click(object sender, EventArgs e)
        {
            objectNamePublic = "PAY";
            actionPublic = "REQUEST";
            ServerLoggingPay();
        }

        public void ServerLoggingPay()
        {
            Thread tClient = new Thread(new ThreadStart(PayRequestLog));
            tClient.Start();
        }

        public void PayRequestLog()
        {
            try
            {
                string time = Utilities.GetNow();
                string message = Utilities.CreateMessage(tablesIdPublic, tablesNamePublic, invoiceIdPublic, menuIdPublic, menuNamePublic, objectNamePublic, actionPublic, numOldPublic, numNewPublic, usernamePublic, fullnamePublic, groupnamePublic, time);

                MyUdpClient client = new MyUdpClient();
                client.OnConnect(message);
            }
            catch
            {
                MessageBox.Show("Chưa gửi được !");
            }
        }

        private void gridInvoiceDetail_Click(object sender, EventArgs e)
        {
            int rowIndex = gridInvoiceDetail.CurrentRowIndex;
            menuIdPublic = gridInvoiceDetail[rowIndex, 0].ToString();
            menuNamePublic = gridInvoiceDetail[rowIndex, 1].ToString();
        }
    }
}